package com.honey.compilation.properties;

import java.util.ArrayList;
import java.util.List;

import com.honey.core.builder.CharacterBuilder;
import com.honey.core.builder.CharacterBuilderFactory;
import com.honey.core.compiler.Compilation;
import com.honey.core.compiler.CompilationException;
import com.honey.core.compiler.IndentSpace;
import com.honey.core.types.FullyQualifiedJavaType;
import com.honey.core.utils.StringUtility;

/**
 * Properties 文件编译器
 * @author Administrator
 *
 */
public class PropertiesCompilation implements Compilation{
	
	/** propertie节点文件 */
	private List<PropertieElement> elements;

	/** 文件注释 */
	private List<String> fileComments;
	
	/** 文件尾部注释 */
	private List<String> afterBodyComments;
	
	/** 包路径和名称 */
	private FullyQualifiedJavaType type= null;
	
	/**
	 * 构造函数
	 * @param type 包路径和名称,和java类中type一致
	 */
	public PropertiesCompilation( FullyQualifiedJavaType type ){
		elements = new ArrayList<PropertieElement>();
		fileComments = new ArrayList<String>();
		afterBodyComments = new ArrayList<String>();
	}

	/**
	 * 构造函数
	 * @param comments
	 * @throws CompilationException
	 */
	public PropertiesCompilation(FullyQualifiedJavaType type , String ...comments ) throws CompilationException{
		this(type);
		addFileComment(comments);
	}
	
	/**
	 * 添加一个键值对
	 * @param key
	 * @param value
	 * @throws CompilationException
	 */
	public void addKeyValueElement(String key ,String value) throws CompilationException{
		if(StringUtility.stringHasValue(key) ){
			elements.add(new KeyValueElement(key,value ));
		}
	}
	
	@Override
	public void addFileComment(String... comments) throws CompilationException {
		for(String comment :  comments){
			if( comment!= null)
				this.fileComments.add( comment  );
    	}		
	}
	
	public void addComment(String ...comments){
		this.elements.add(new CommentsElement(comments));
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.honey.core.dom.Compilation#getFileComments()
	 */
	@Override
	public List<String> getFileComments() throws CompilationException {
		
		return this.fileComments;
	}

	/*
	 * (non-Javadoc)
	 * @see com.honey.core.compiler.Compilation#addAfterBodyComment(java.lang.String[])
	 */
	@Override
	public void addAfterBodyComment(String... comments) throws CompilationException {
		for(String comment :  comments){
			if( comment!= null)
				this.afterBodyComments.add( comment  );
    	}	
	}
	
	public List<String> getAfterBodyComments() throws CompilationException {
		
		return this.afterBodyComments;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.honey.core.dom.Compilation#compiledContent()
	 */
	@Override
	public CharacterBuilder compiledContent() throws CompilationException {
		CharacterBuilder answer = CharacterBuilderFactory.createC16StringBuilder();
		
		//注释
		for(String comment : fileComments ){
			answer.append("#").append(comment);
			IndentSpace.newLine(answer);
		}
		if(fileComments.size() > 0 ){
			IndentSpace.newLine(answer);
		}

		for(PropertieElement element : elements ){
			answer.append(element.compiledContent());
			IndentSpace.newLine(answer);
		}
		
		for(String comment : afterBodyComments ){
			IndentSpace.newLine(answer);
			answer.append("#").append(comment);
		}
		return answer;
	}
	
	@Override
	public FullyQualifiedJavaType getType() {
		return type;
	}
}
